import Title from '@components/title'
import Line from './components/line'
import OrderLine from '@components/orderLine'
import Claim from './components/claim'
import Evidence from './components/evidence'
import style from './index.module.less'
import { useMemo } from 'react'
import { formatDate } from '@src/assets/js/tool'

// 获得仲裁员
function getArbitrators(d) {
  if (!d) {
    return ''
  }
  const list = []
  if (d.arbitrator) {
    list.push(d.arbitrator)
  }
  if (d.arbitrator1) {
    list.push(d.arbitrator1)
  }
  if (d.arbitrator2) {
    list.push(d.arbitrator2)
  }
  return list.join('、')
}
function Main ({ data }) {
  const arbitrators = useMemo(() => {
    return getArbitrators(data)
  }, [data])
  const applicants = useMemo(() => {
    return data && data.applicants instanceof Array ? data.applicants : []
  }, [data])
  const applicantAgents = useMemo(() => {
    return data && data.applicant_agents instanceof Array ? data.applicant_agents : []
  }, [data])
  const respondents = useMemo(() => {
    return data && data.respondents instanceof Array ? data.respondents : []
  }, [data])
  const respondentAgents = useMemo(() => {
    return data && data.respondents_agents instanceof Array ? data.respondents_agents : []
  }, [data])
  const { status, className } = useMemo(() => {
    const className = [style.status]
    if (!data) {
      return { status: '', className }
    }
    let s = data.status

    if (s === '待审核') {
      className.push(style.pass)
    } else if (s === '已受理') {
      className.push(style.wait)
    } else if (['送达中', '审理中'].includes(s)) {
      className.push(style.wait)
    } else if (['已结案', '不予受理', '已归档'].includes(s)) {
      className.push(style.close)
    } else if (s === '审核不通过') {
      className.push(style.unpass)
    } else {
      className.push(style.unsubmit)
    }
    return {
      status: s,
      className: className.join(' ')
    }
  }, [data])
  return (
    <div className={style.container}>
      <Title title='案件信息' />
      <Line label='案号' value={data.caseno_desc || '无'} />
      <Line label='状态' value={<div className={className}>{status}</div>} />
      <Line label='案由' value={data.cause_action || '无'} />
      {data.hearing_datetime && <Line label='开庭时间' value={formatDate(data.hearing_datetime, 'YYYY.MM.DD')} />}
      {arbitrators && <Line label='仲裁庭' value={arbitrators} />}
      {data.processer && <Line label='办案秘书' value={data.processer} />}
      {
        applicants.length > 0 && (
          <>
            <Title title='申请人' />
            {
              applicants.map((li, i) => <OrderLine key={i} index={i + 1} value={li.name} />)
            }
          </>
        )
      }
      {
        applicantAgents.length > 0 && (
          <>
            <Title title='申请人代理人' />
            {
              applicantAgents.map((li, i) => <OrderLine key={i} index={i + 1} value={li.name} third />)
            }
          </>
        )
      }
      {
        respondents.length > 0 && (
          <>
            <Title title='被申请人' />
            {
              respondents.map((li, i) => <OrderLine key={i} index={i + 1} value={li.name} second />)
            }
          </>
        )
      }
      {
        respondentAgents.length > 0 && (
          <>
            <Title title='被申请人代理人' />
            {
              respondentAgents.map((li, i) => <OrderLine key={i} index={i + 1} value={li.name} third />)
            }
          </>
        )
      }
      {
        data.basis && (
          <>
            <Title title='仲裁依据' />
            <div className={style.content}>{data.basis}</div>
          </>
        )
      }
      {
        data.claims.map((li, i) => {
          return (
            <Claim data={li} key={i} />
          )
        })
      }
      {
        data.cclaims.map((li, i) => {
          return (
            <Claim data={li} key={i} anti />
          )
        })
      }
      {
        data.evidence && (
          <Evidence data={data} />
        )
      }
    </div>
  )
}

export default Main
